package com.kolon.business.mKolonInt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonInt.controller.MKolonIntSearchListMoreController")
public class MKolonIntSearchListMoreController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mKolonInt/mKolonIntSearchListMore.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
			
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		
		List mapList = new ArrayList();
		Map map = new HashMap();
		
		// 검색 미사용
		String Page = ComUtil.getNullCheck(request.getParameter("Page"), "1");
						
		buffer.append("/kolon_int.aspx?strMode=").append("18");
		buffer.append("&SortFieldName=").append("SortCreateDate");
		buffer.append("&SortAscDesc=").append("desc");
		buffer.append("&Page=").append(Page);
		buffer.append("&ViewRowCount=15");
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
			
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardList");
		Iterator itXml = xmlList.iterator();
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ArticleCD", el.getChild("ArticleCD").getText());
			map.put("RowNum", el.getChild("RowNum").getText());
			map.put("UserID", el.getChild("UserID").getText());
			map.put("UserName", el.getChild("UserName").getText());
			map.put("BoardCD", el.getChild("BoardCD").getText());
			map.put("Title", ComUtil.subString(el.getChild("Title").getText(),15));
			map.put("SelReply", el.getChild("SelReply").getText());
			map.put("UCreateDate", el.getChild("UCreateDate").getText().substring(0,10));
			map.put("ExpireDate", el.getChild("ExpireDate").getText());
			map.put("ViewCnt", ComUtil.addComma(el.getChild("ViewCnt").getText()));
			map.put("FileOnly", el.getChild("FileOnly").getText());
			map.put("Importance", el.getChild("Importance").getText());
			map.put("AttachCNT", el.getChild("AttachCNT").getText());
			map.put("CommentCNT", el.getChild("CommentCNT").getText());
			map.put("Ref_Seq", el.getChild("Ref_Seq").getText());
			map.put("Ref_Level", el.getChild("Ref_Level").getText());
			map.put("SubCnt", el.getChild("SubCnt").getText());
			map.put("IsNotice", el.getChild("IsNotice").getText());
			map.put("B_ACL", el.getChild("B_ACL").getText());
			map.put("Gubun", el.getChild("Gubun").getText());
			map.put("Company", el.getChild("Company").getText());
			map.put("Price", el.getChild("Price").getText());
			map.put("Area", el.getChild("Area").getText());
			map.put("Status", el.getChild("Status").getText());
			map.put("Phone", el.getChild("Phone").getText());
			map.put("Address", el.getChild("Address").getText());
			map.put("Theme", el.getChild("Theme").getText());
			map.put("Point", el.getChild("Point").getText());
			map.put("UCStatus", el.getChild("UCStatus").getText());
			map.put("TelOffice", el.getChild("TelOffice").getText());
			map.put("Mobile", el.getChild("Mobile").getText());
			mapList.add(map);
		}

		
		// TableInfo 영역
		Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("TableInfo");
		Iterator itXml2 = xmlList2.iterator();
		Map map2 = new HashMap();
		// XML 데이터 
		if (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map2 = new HashMap();
			map2.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			map2.put("TotalNoticeRecortCnt", el.getChild("TotalNoticeRecortCnt").getText());
			map2.put("TotalPage", el.getChild("TotalPage").getText());
			map2.put("CurrPage", el.getChild("CurrPage").getText());
			map2.put("OnePageCnt", el.getChild("OnePageCnt").getText());
			map2.put("bPublicWrite", el.getChild("bPublicWrite").getText());
			map2.put("UserID", el.getChild("UserID").getText());
			map2.put("SAdminCNT", el.getChild("SAdminCNT").getText());
		}

		
		Map model = new HashMap();
		model.put("list", mapList);
		model.put("map", map2);
		
		ModelAndView modelAndView = new ModelAndView();
				
		modelAndView.addObject("strMode", "18");
		modelAndView.addObject("BoardCD", request.getParameter("BoardCD"));
		modelAndView.addObject("B_IsSub", request.getParameter("B_IsSub"));
		modelAndView.addObject("Depth", request.getParameter("Depth"));
		
		// 리턴
		return new ModelAndView("jsonView", model);
	}

}